/**
 * 添加或者修改页面
 */
var BasicAddressInfoDlg = {
    data: {
        putgoodsName: "",
        putgoodsId: "",
        subsidiary: "",
        address: "",
		longitude:"",
		latitude:"",
        contacts: "",
        tel: "",
        deliverId: "",
        dept_id: "",
        sheng:"",
        shi:"",
        qu:"",
        jie:""
    },
    sid:"",

};
layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();





  form.on('select(xmFilter)',function (data){
      var selectedOption = data.elem[data.elem.selectedIndex];
      var id = selectedOption.getAttribute("id");
      BasicAddressInfoDlg.sid = id;
      console.log(id)
      if ( BasicAddressInfoDlg.sid == ""){
          layui.form.render("select");
      }else {
          shi()
      }
  })
    function shi(){
        form.on('select(xmFilter2)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                qu();
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id: BasicAddressInfoDlg.sid },
            async:false,
            success: function (data) {
                console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select = document.getElementById('shi');
                select.innerHTML = "";
                var select2 = document.getElementById('qu');
                select2.innerHTML = "";
                var select3 = document.getElementById('jie');
                select3.innerHTML = "";
                var defaultOption = document.createElement("option");
                defaultOption.value = "";
                defaultOption.text = "Select City";
                select.add(defaultOption);

                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select.add(option);
                });
                layui.form.render("select");
            }
        })

    }
    function qu(){
        form.on('select(xmFilter3)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == ""){
                layui.form.render("select");
            }else {
                jie();
            }
        })

        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            type: 'get',
            data: {id: BasicAddressInfoDlg.sid },
            async:false,
            success: function (data) {
                console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select2 = document.getElementById('qu');
                select2.innerHTML = " ";
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";

                var defaultOption3 = document.createElement("option");
                defaultOption3.value = "";
                defaultOption3.text = "Select District/County";
                select2.add(defaultOption3);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select2.add(option);
                });
                layui.form.render("select");
            }
        })
    }
    function jie(){
        form.on('select(xmFilter4)',function (data){
            var selectedOption = data.elem[data.elem.selectedIndex];
            var id = selectedOption.getAttribute("id");
            BasicAddressInfoDlg.sid = id;
            if ( BasicAddressInfoDlg.sid == "") {
                layui.form.render("select");
            }
        })
        $.ajax({
            url: Feng.ctxPath + "/basicAddress/list2",
            dataType: 'json',
            data: {id: BasicAddressInfoDlg.sid },
            type: 'get',
            async:false,
            success: function (data) {
                console.log(data);
                //使用循环遍历，给下拉列表赋值
                var select3 = document.getElementById('jie');
                select3.innerHTML = " ";
                var defaultOption2 = document.createElement("option");
                defaultOption2.value = "";
                defaultOption2.text = "Select Street/Town";
                select3.add(defaultOption2);
                data.forEach(function (item) {
                    var option = document.createElement("option");
                    option.value = item.extName; // 设置选项的值
                    option.text = item.extName;
                    option.id = item.id;
                    select3.add(option);
                });
                layui.form.render("select");
            }
        })
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicAddress/addItem", function (data) {
            Feng.success("Add successful!");
        	var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
        }, function (data) {
            Feng.error("Add failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //选择位置
    $("#selectlocation").click(function(){
        layer.open({
            type: 2,
            title: 'Select Address',
            area: ['800px', '550px'],
            content: Feng.ctxPath + '/basicAddress/toChoiceAddress?address=' + $("#address").val()+"&longitude=" + $("#longitude").val()+"&latitude=" + $("#latitude").val(),
            end: function () {
				if(BasicAddressInfoDlg.data.address != null && BasicAddressInfoDlg.data.address != ''){
	                $("#address").val(BasicAddressInfoDlg.data.address);//地址
	                $("#longitude").val(BasicAddressInfoDlg.data.longitude);//经度
	                $("#latitude").val(BasicAddressInfoDlg.data.latitude);//维度
				}
            }
        });
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});
